#!/usr/bin/env python3

import json
import os

import requests
import scrapy
from scrapy_redis.spiders import RedisSpider

from zuwome.items import UserItem

def writeToFile(filename, data):
    with open(filename, 'wb') as f:
        for chunk in data:
            f.write(chunk)

def getUids():
    uids = []
    with open("userData/uids.txt", "r") as f:
        for uid_line in f.readlines():
            uids.append(uid_line.split()[1])
    return uids

def parsePicUrls(data):
    pic_urls = []
    photos_list = data['data']['photos']
    print(type(photos_list))
    for photo_item in photos_list:
        pic_url = photo_item['url']
        pic_urls.append(pic_url)

    return pic_urls

def parseVideoUrls(data):
    sk_base_video = data['data']['base_video']['sk']
    if "video" not in sk_base_video.keys():
        return None
    video_url = sk_base_video['video']['video_url']

    return video_url

def downloadPic(uid, pic_url):
    """
    根据图片的路径下载图片
    """
    r = requests.get(url=pic_url, stream=True)
    filename = "userData/pictures/" + uid + ".jpg"
    if os.path.exists(filename):
        return "file exists, please check!!!"
    writeToFile(filename, r)

def downloadVideo(uid, video_url):
    """
    根据视频的路径下载图片
    """
    r = requests.get(url=video_url, stream=True)
    filename = "userData/videos/" + uid + ".mp4"
    if os.path.exists(filename):
        return "file exists, please check!!!"
    writeToFile(filename, r)

class userSpider(RedisSpider):
    name = "downinfos"

    redis_key = "user_url:start_urls"

    def __init__(self, *args, **kwargs):
        self.allowed_domains = "v2.zuwome.com"
        super(userSpider, self).__inti__(*args, **kwargs)


    def parse(self, response):
        uid = response.meta['uid']
        userinfo = json.loads(response.text)
        #  保存截获的个人json数据包
        with open("userData/personal/u_%s.txt" % (uid), "a+") as f:
            f.write(json.dumps(userinfo, ensure_ascii=False))

        #  print(type(userinfo['data']['realname']))
        #  print(userinfo['data']['realname'].keys())
        #  print(userinfo['data']['realname']['name'])
        #  print(userinfo['data']['address'].keys())
        #  print(userinfo['data']['nickname'])
        #  print(userinfo['data']['base_video']['sk']['video'].keys())
        #  print(userinfo['data']['base_video']['sk']['video']['video_url'])

        #  保存个人图片
        pic_urls = parsePicUrls(userinfo)
        for i in range(len(pic_urls)):
            downloadPic(uid + "_" + str(i), pic_urls[i])

        #  保存个人视频
        video_url = parseVideoUrls(userinfo)
        if video_url is not None:
            downloadVideo(uid, video_url)

        yield UserItem(uid=uid)


